/*
IE6/7 不支持在当前 document 创建的 object append到另外一个 document
 */
JObj.use("ui");
JObj.use("JAnimate");

JObj.UI.JModalLayer = {};

(function($){

    var objs = {
        oOutline:null,
        oMask:null,
        oIfr:null,
        oCnt:null,
        ani:null
    };

    var vars = {
        bgColor:'#999999',
        alpha:70,
        orgOverflow:null,
        showed:false
    };

    $.onShow = null;
    $.onHidden = null;

    var window_resize = function(){
        JObj.css(objs.oMask,{top:0,left:0,width:Math.max(document.documentElement.scrollWidth,document.documentElement.clientWidth),height:Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight)});
    }

    var create = function(){
        objs.oOutline = JObj.$c('DIV');
        document.body.appendChild(objs.oOutline);
        JObj.css(objs.oOutline,{width:'100%',display:'none',position:'absolute',top:0,left:0,zIndex:(JObj.UI.JLayerManager.MAX_ZINDEX - JObj.UI.JLayerManager.ZINDEX_STEP)});

        objs.oIfr = JObj.$c('IFRAME');
        objs.oOutline.appendChild(objs.oIfr);        
        
        objs.oMask = JObj.$c('DIV');
        objs.oOutline.appendChild(objs.oMask);

        objs.oCnt = JObj.$c('DIV');
        objs.oOutline.appendChild(objs.oCnt);
        JObj.css(objs.oCnt,{width:'100%',position:'absolute',top:0});//IE,Opera下，给它设置 absolute 是为了让它 hasLayout,这样，它可以应用 alpha

        objs.ani = JObj.UI.JAnimate.$(objs.oMask);
        objs.ani.setFrequence(10);
        JObj.css(objs.oMask,{position:'absolute',backgroundColor:vars.bgColor});

        JObj.addEvent(objs.oMask,'dblclick',$.hidden);
    }
    
	$.show = function(){
		if(objs.oMask == null)
			create();
		
		if(!vars.showed){
	        vars.orgOverflow = document.documentElement.style.overflow;
			JObj.css(document.documentElement,{overflow:'hidden'});
	        JObj.css(objs.oOutline,{display:'block'});
	        JObj.css(objs.oIfr,{position:"absolute",alpha:0,top:0,left:0,width:Math.max(document.documentElement.scrollWidth,document.documentElement.clientWidth),height:Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight)});
	        JObj.css(objs.oMask,{top:0,left:0,width:Math.max(document.documentElement.scrollWidth,document.documentElement.clientWidth),height:Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight)});
	        
	        JObj.isFunction($.onShow) && $.onShow(objs.oMask);
	        JObj.addEvent(window,'resize',window_resize);
	        objs.ani.run({alpha:10},{alpha:vars.alpha});
		}
		vars.showed = true;
    }

    var hidden_complete = function(){
        JObj.removeEvent(window,'resize',window_resize)
        JObj.isFunction($.onHidden) && $.onHidden(objs.oMask);
        JObj.css(objs.oOutline,{display:'none'});
        //JObj.css(objs.oCnt,{alpha:100});
        objs.oCnt.style.display = "block";
        document.documentElement.style.overflow = vars.orgOverflow;

        objs.ani.onComplete = null;
        objs.ani.onProcess = null;
    }

/*    var hidden_process = function(obj,fromRule,toRule){
        JObj.css(objs.oCnt,{alpha:fromRule.alpha});
    }*/

    $.hidden = function(){
    	vars.showed = false;
        //objs.ani.onProcess = hidden_process;
    	objs.oCnt.style.display = "none";
        objs.ani.onComplete = hidden_complete;
        objs.ani.run({alpha:vars.alpha},{alpha:0});
    }

    $.getLayer = function(){
        return objs.oCnt;
    }
})(JObj.UI.JModalLayer);